package com.atguigu.tingshu.order.receiver;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.order.service.MqOpsService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class OrderInfoReceiver {
    @Autowired
    private MqOpsService mqOpsService;
    // MqConst.EXCHANGE_LOCAL_MSG  MqConst.ROUTING_LOCAL_MSG  orderNo
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(
                    value = MqConst.EXCHANGE_LOCAL_MSG,
                    durable = "true"
            ),
            value = @Queue(
                    value = MqConst.QUEUE_LOCAL_MSG,
                    durable = "true",
                    exclusive = "false"
            ),
            key = MqConst.ROUTING_LOCAL_MSG
    ))
    @SneakyThrows
    public void listenLocalMsg(String content, Message message, Channel channel){
        if (!StringUtils.hasText(content)){
            return;
        }
        mqOpsService.updateLocalMsg(content);

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        // 3.手动应答
        channel.basicAck(deliveryTag, false);

    }
}
